{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e6fd05db-21c2-4227-9900-0840bc62cb31",
   "metadata": {},
   "source": [
    "# Polygon IO Toolkit\n",
    "\n",
    "This notebook shows how to use agents to interact with the [Polygon IO](https://polygon.io/) toolkit. The toolkit provides access to Polygon's Stock Market Data API."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4da342d",
   "metadata": {},
   "source": [
    "## Example Use\n",
    "\n",
    "\n",
    "### Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c17b33e0",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install --upgrade --quiet langchain-community > /dev/null"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cd00ad2",
   "metadata": {},
   "source": [
    "Get your Polygon IO API key [here](https://polygon.io/), and then set it below.\n",
    "Note that the tool used in this example requires a \"Stocks Advanced\" subscription"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a180a2b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "········\n"
     ]
    }
   ],
   "source": [
    "import getpass\n",
    "import os\n",
    "\n",
    "os.environ[\"POLYGON_API_KEY\"] = getpass.getpass()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed6f89fa",
   "metadata": {},
   "source": [
    "It's also helpful (but not needed) to set up [LangSmith](https://smith.langchain.com/) for best-in-class observability"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56670cf6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
    "# os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d93e6bd-03d7-4d3c-b915-8b73164e2ad8",
   "metadata": {},
   "source": [
    "### Initializing the agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "648a2cb2-308e-4b2e-9b73-37109be4e258",
   "metadata": {
    "is_executing": true
   },
   "outputs": [],
   "source": [
    "from langchain import hub\n",
    "from langchain.agents import AgentExecutor, create_openai_functions_agent\n",
    "from langchain_community.agent_toolkits.polygon.toolkit import PolygonToolkit\n",
    "from langchain_community.utilities.polygon import PolygonAPIWrapper\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI(temperature=0)\n",
    "\n",
    "instructions = \"\"\"You are an assistant.\"\"\"\n",
    "base_prompt = hub.pull(\"langchain-ai/openai-functions-template\")\n",
    "prompt = base_prompt.partial(instructions=instructions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "18650040-0ff8-4c0f-a4f2-be6aad7fe63e",
   "metadata": {},
   "outputs": [],
   "source": [
    "polygon = PolygonAPIWrapper()\n",
    "toolkit = PolygonToolkit.from_polygon_api_wrapper(polygon)\n",
    "agent = create_openai_functions_agent(llm, toolkit.get_tools(), prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fd7463e4-8716-4d1d-860a-770533eaa742",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor = AgentExecutor(\n",
    "    agent=agent,\n",
    "    tools=toolkit.get_tools(),\n",
    "    verbose=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71f05fc9-d80d-4614-b9a3-e0a5e43cbbbb",
   "metadata": {},
   "source": [
    "### Get the last price quote for a stock"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b97409f3-dc87-425d-b555-406cf8466a28",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent_executor.invoke({\"input\": \"What is the latest stock price for AAPL?\"})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9e666ee1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
